package com.zimmor.mq.rocket.order;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2021/11/18 14:34
 * @Description 发送同步消息
 * @version: 1.0.0
 */

public class Producer {
    public static void main(String[] args) throws Exception {
       //发送消息的步骤
       //1.创建一个producer 设置producer的生产者组名groupID，
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //2.设置namesrv地址
        producer.setNamesrvAddr("192.168.204.50:9876");
       //3.启动producer
        producer.start();
        List<OrderStep> orderSteps = new OrderStep().buildOrders();
        for (OrderStep orderStep : orderSteps ) {

            Message message = new Message("order", "tag1", orderStep.toString().getBytes());
            //5.发送消息
            SendResult result = producer.send(message, new MessageQueueSelector() {
                public MessageQueue select(List<MessageQueue> list, Message message, Object id) {
                    long index = (Long) id % list.size();
                    MessageQueue messageQueue = list.get((int) index);
                    return messageQueue;

                }
            }, orderStep.getOrderId());
            System.out.println("result = " + result);
        }
       //6.关闭producer
        producer.shutdown();
    }
}
